package db.dbTables;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import core.objects.BaseObject;
import core.objects.User;
import core.objects.enums.GenderEnum;
import db.MySQLFormatDate;

public class UserTable extends AbstractDBTable {

    public static final String COLUMN_ID = "user_id";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_FIRST_NAME = "first_name";
    public static final String COLUMN_LAST_NAME = "last_name";
    public static final String COLUMN_PASSWORD = "password";
    public static final String COLUMN_GENDER = "gender";
    public static final String COLUMN_BIRTHDAY = "birthday";
    public static final String COLUMN_CITY_ID = "city_id";
    public static final String COLUMN_COUNTRY_ID = "country_id";
    public static final String COLUMN_IS_ADMIN = "is_admin";
    public static final String COLUMN_REGISTER_TIME = "register_time";
    public static final String COLUMN_IMAGE = "image";
    public static final String COLUMN_EMAIL = "email";

    final public DbColumn id = new DbColumn(COLUMN_ID, Types.INTEGER, 11, false, this);
    final public DbColumn username = new DbColumn(COLUMN_USERNAME, Types.VARCHAR, 45, false, this);
    final public DbColumn first_name = new DbColumn(COLUMN_FIRST_NAME, Types.VARCHAR, 45, true, this);
    final public DbColumn last_name = new DbColumn(COLUMN_LAST_NAME, Types.VARCHAR, 45, true, this);
    final public DbColumn password = new DbColumn(COLUMN_PASSWORD, Types.VARCHAR, 40, false, this); // should be char
    final public DbColumn gender = new DbColumn(COLUMN_GENDER, Types.OTHER, 1, true, this);
    final public DbColumn birthday = new DbColumn(COLUMN_BIRTHDAY, Types.DATE, 4, true, this);
    final public DbColumn city_id = new DbColumn(COLUMN_CITY_ID, Types.INTEGER, 11, true, this);
    final public DbColumn country_id = new DbColumn(COLUMN_COUNTRY_ID, Types.INTEGER, 11, true, this);
    final public DbColumn is_admin = new DbColumn(COLUMN_IS_ADMIN, Types.BIT, 1, false, this);
    final public DbColumn registerTime = new DbColumn(COLUMN_REGISTER_TIME, Types.TIMESTAMP, 1, false, this);
    final public DbColumn image = new DbColumn(COLUMN_IMAGE, Types.BLOB, 1, true, this);
    final public DbColumn email = new DbColumn(COLUMN_EMAIL, Types.VARCHAR, 100, true, this);

    public UserTable() {
        super("user");
        setIDColumn(id);
    }

    @Override
    public List<DbColumn> getDbColumns() {
        List<DbColumn> list = new ArrayList<DbColumn>();
        list.add(id);
        list.add(username);
        list.add(first_name);
        list.add(last_name);
        list.add(password);
        list.add(gender);
        list.add(birthday);
        list.add(city_id);
        list.add(country_id);
        list.add(is_admin);
        list.add(registerTime);
        list.add(image);
        list.add(email);
        return list;
    }

    @Override
    public BaseObject convertSqlTableToBaseObject(ResultSet resultSet) throws SQLException {
        Calendar birthday = null;
        
        int id = resultSet.getInt(COLUMN_ID);
        String username = resultSet.getString(COLUMN_USERNAME);
        String password = resultSet.getString(COLUMN_PASSWORD);
        String firstname = resultSet.getString(COLUMN_FIRST_NAME);
        String lastname = resultSet.getString(COLUMN_LAST_NAME);
        if (resultSet.getString(COLUMN_BIRTHDAY) != null) {
            birthday = MySQLFormatDate.toCalendar(resultSet.getString(COLUMN_BIRTHDAY));
        }
        int cityId = resultSet.getInt(COLUMN_CITY_ID);
        int countryId = resultSet.getInt(COLUMN_COUNTRY_ID);
        GenderEnum gender = GenderEnum.valueOfString((String)resultSet.getObject(COLUMN_GENDER));
        Timestamp registerTime = resultSet.getTimestamp(COLUMN_REGISTER_TIME);
        byte[] imageByteArr = getByteArrFromBlob(resultSet.getBlob(COLUMN_IMAGE));
        String email = resultSet.getString(COLUMN_EMAIL);
        return new User(id, username, firstname, lastname, password, gender,
                birthday, cityId, countryId, registerTime, imageByteArr, email);
    }

    private byte[] getByteArrFromBlob(Blob blob) throws SQLException {
        if (blob == null){
            return null;
        }
        byte imageByteArr[] = new byte[(int) blob.length()];//an array is created but contains no data
        imageByteArr = blob.getBytes(1, (int) blob.length());
        return imageByteArr;
    }
}
